1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import java.util.Collection;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import javax.annotation.Nullable;
26  
27  /**
28   * A {@code Multimap} that cannot hold duplicate key-value pairs. Adding a
29   * key-value pair that's already in the multimap has no effect. See the {@link
30   * Multimap} documentation for information common to all multimaps.
31   *
32   * <p>The {@link #get}, {@link #removeAll}, and {@link #replaceValues} methods
33   * each return a {@link Set} of values, while {@link #entries} returns a {@code
34   * Set} of map entries. Though the method signature doesn't say so explicitly,
35   * the map returned by {@link #asMap} has {@code Set} values.
36   *
37   * <p>If the values corresponding to a single key should be ordered according to
38   * a {@link java.util.Comparator} (or the natural order), see the
39   * {@link SortedSetMultimap} subinterface.
40   * 
41   * <p>Since the value collections are sets, the behavior of a {@code SetMultimap}
42   * is not specified if key <em>or value</em> objects already present in the 
43   * multimap change in a manner that affects {@code equals} comparisons.  
44   * Use caution if mutable objects are used as keys or values in a 
45   * {@code SetMultimap}.
46   *
47   * <p>See the Guava User Guide article on <a href=
48   * "http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multimap">
49   * {@code Multimap}</a>.
50   *
51   * @author Jared Levy
52   * @since 2.0 (imported from Google Collections Library)
53   */
54  @GwtCompatible
55  public interface SetMultimap<K, V> extends Multimap<K, V> {
56    /**
57     * {@inheritDoc}
58     *
59     * <p>Because a {@code SetMultimap} has unique values for a given key, this
60     * method returns a {@link Set}, instead of the {@link java.util.Collection}
61     * specified in the {@link Multimap} interface.
62     */
63    @Override
64    Set<V> get(@Nullable K key);
65  
66    /**
67     * {@inheritDoc}
68     *
69     * <p>Because a {@code SetMultimap} has unique values for a given key, this
70     * method returns a {@link Set}, instead of the {@link java.util.Collection}
71     * specified in the {@link Multimap} interface.
72     */
73    @Override
74    Set<V> removeAll(@Nullable Object key);
75  
76    /**
77     * {@inheritDoc}
78     *
79     * <p>Because a {@code SetMultimap} has unique values for a given key, this
80     * method returns a {@link Set}, instead of the {@link java.util.Collection}
81     * specified in the {@link Multimap} interface.
82     *
83     * <p>Any duplicates in {@code values} will be stored in the multimap once.
84     */
85    @Override
86    Set<V> replaceValues(K key, Iterable<? extends V> values);
87  
88    /**
89     * {@inheritDoc}
90     *
91     * <p>Because a {@code SetMultimap} has unique values for a given key, this
92     * method returns a {@link Set}, instead of the {@link java.util.Collection}
93     * specified in the {@link Multimap} interface.
94     */
95    @Override
96    Set<Map.Entry<K, V>> entries();
97  
98    /**
99     * {@inheritDoc}
100    *
101    * <p><b>Note:</b> The returned map's values are guaranteed to be of type
102    * {@link Set}. To obtain this map with the more specific generic type
103    * {@code Map<K, Set<V>>}, call {@link Multimaps#asMap(SetMultimap)} instead.
104    */
105   @Override
106   Map<K, Collection<V>> asMap();
107 
108   /**
109    * Compares the specified object to this multimap for equality.
110    *
111    * <p>Two {@code SetMultimap} instances are equal if, for each key, they
112    * contain the same values. Equality does not depend on the ordering of keys
113    * or values.
114    *
115    * <p>An empty {@code SetMultimap} is equal to any other empty {@code
116    * Multimap}, including an empty {@code ListMultimap}.
117    */
118   @Override
119   boolean equals(@Nullable Object obj);
120 }